package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import test.serverframe.armc.server.manager.controller.vo.TaskInvokeVo;
import test.serverframe.armc.server.manager.domain.TaskInvoke;
import test.serverframe.armc.server.manager.domain.params.InvokePage;
import test.serverframe.armc.server.manager.domain.params.InvokePageParams;
import test.serverframe.armc.server.manager.domain.params.UpdateInvokeStatusParams;

import java.util.List;

public interface TaskInvokeMapper {
    @Delete({
            "delete from tb_ssgp_task_invoke",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String taskInvokeId);

    @Insert({
            "insert into tb_ssgp_task_invoke (task_invoke_id, node_arr",
            "class_name,method_name,params,invoke_name, failed_strategy",
            "remark, start_time, end_time, updata_time, status, is_used, is_concurrent, execute_plan_data)",
            "values (#{taskInvokeId,jdbcType=VARCHAR}, #{nodeArr,jdbcType=VARCHAR}, #{className,jdbcType=VARCHAR},",
            "#{methodName,jdbcType=VARCHAR},#{params,jdbcType=VARCHAR},#{invokeName,jdbcType=VARCHAR}, #{failedStrategy,jdbcType=VARCHAR}",
            "#{remark,jdbcType=VARCHAR}, #{startTime,jdbcType=TIMESTAMP},#{endTime,jdbcType=TIMESTAMP}, #{updataTime,jdbcType=TIMESTAMP}",
            "#{status,jdbcType=BIT}, #{isUsed,jdbcType=BIT},#{isConcurrent,jdbcType=BIT},#{executePlanData,jdbcType=VARCHAR})"
    })
    int insert(TaskInvoke record);

    @InsertProvider(type = TaskInvokeSqlProvider.class, method = "insertSelective")
    int insertSelective(TaskInvoke record);

    @Select({
            "select",
            "task_invoke_id, class_name, method_name, params, invoke_name, remark, updata_time, failed_strategy,",
            "start_time, end_time, status, is_concurrent, execute_plan_data, node_arr",
            "from tb_ssgp_task_invoke",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR} and is_used = 1"
    })
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "class_name", property = "className", jdbcType = JdbcType.VARCHAR),
            @Result(column = "method_name", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params", property = "params", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "updata_time", property = "updataTime", jdbcType = JdbcType.TINYINT),
            @Result(column = "failed_strategy", property = "failedStrategy", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_concurrent", property = "isConcurrent", jdbcType = JdbcType.BIT),
            @Result(column = "execute_plan_data", property = "executePlanData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "node_arr", property = "nodeArr", jdbcType = JdbcType.VARCHAR)
            })
    TaskInvoke selectByPrimaryKey(String taskInvokeId);

    @UpdateProvider(type = TaskInvokeSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(TaskInvoke record);

    @Update({
            "update tb_ssgp_task_invoke",
            "set invoke_name = #{invokeName,jdbcType=VARCHAR},",
            "remark = #{remark,jdbcType=VARCHAR},",
            "start_time = #{startTime,jdbcType=TIMESTAMP},",
            "end_time = #{endTime,jdbcType=TIMESTAMP},",
            "updata_time = #{updataTime,jdbcType=TIMESTAMP},",
            "status = #{status,jdbcType=BIT},",
            "is_used = #{isUsed,jdbcType=BIT},",
            "failed_strategy = #{failedStrategy,jdbcType=VARCHAR},",
            "is_concurrent = #{isConcurrent,jdbcType=BIT},",
            "execute_plan_data = #{executePlanData,jdbcType=VARCHAR},",
            "node_arr = #{nodeArr,jdbcType=VARCHAR},",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(TaskInvoke record);

    @SelectProvider(type = TaskInvokeSqlProvider.class, method = "selectByPage")
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "last_task_success_total", property = "lastTaskSuccessTotal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "last_task_total", property = "lastTaskTotal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "last_start_time", property = "lastStartTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "updata_time", property = "updataTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "last_invoke_record_id", property = "lastInvokeRecordId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "execute_plan_data", property = "executePlanData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "last_status", property = "lastStatus", jdbcType = JdbcType.BIT),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "total", property = "total", jdbcType = JdbcType.INTEGER),
            @Result(column = "tag", property = "tag", jdbcType = JdbcType.VARCHAR),
//            @Result(column="group_id", property="taskGroupId", jdbcType=JdbcType.VARCHAR),
//            @Result(column="ver", property="ver", jdbcType=JdbcType.BIGINT),

    })
    List<InvokePage> selectByPage(InvokePageParams params);

    @Select({
            "select",
            "task_invoke_id, class_name, method_name, params, invoke_name, remark, is_concurrent, execute_plan_data, ",
            "start_time, end_time, updata_time, status, failed_strategy, node_arr",
            "from tb_ssgp_task_invoke",
            "where status = 1 and is_used = 1"
    })
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "class_name", property = "className", jdbcType = JdbcType.VARCHAR),
            @Result(column = "method_name", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params", property = "params", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "updata_time", property = "updataTime", jdbcType = JdbcType.TINYINT),
            @Result(column = "failed_strategy", property = "failedStrategy", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_concurrent", property = "isConcurrent", jdbcType = JdbcType.BIT),
            @Result(column = "execute_plan_data", property = "executePlanData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "node_arr", property = "nodeArr", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_invoke_id", property = "taskEnterInvokes", many = @Many(
                    select = "test.serverframe.armc.server.manager.dao.mapper.TaskEnterInvokeMapper.selectByTaskInvokeId"
            ))
    })
    List<TaskInvoke> selectAll();

    @Select({
            "select",
            "task_invoke_id, class_name, method_name, params, invoke_name, remark, node_arr,",
            "start_time, end_time, updata_time, status, failed_strategy, is_concurrent, execute_plan_data",
            "from tb_ssgp_task_invoke",
            "where status = 1 and is_used = 1 and task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "class_name", property = "className", jdbcType = JdbcType.VARCHAR),
            @Result(column = "method_name", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params", property = "params", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "updata_time", property = "updataTime", jdbcType = JdbcType.TINYINT),
            @Result(column = "failed_strategy", property = "failedStrategy", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_concurrent", property = "isConcurrent", jdbcType = JdbcType.BIT),
            @Result(column = "execute_plan_data", property = "executePlanData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "node_arr", property = "nodeArr", jdbcType = JdbcType.VARCHAR)
           /* @Result(column = "task_invoke_id",property = "taskEnterInvokes",many = @Many(
                    select = "test.serverframe.armc.server.manager.dao.mapper.TaskEnterInvokeMapper.selectByTaskInvokeId"
            ))*/
    })
    TaskInvoke selectAllByTaskInvokeId(String taskInvokeId);

    @Select({
            "select",
            "task_invoke_id, invoke_name, remark, node_arr,",
            "status, failed_strategy, is_concurrent, execute_plan_data",
            "from tb_ssgp_task_invoke",
            "where is_used = 1 and task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "failed_strategy", property = "failedStrategy", jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_concurrent", property = "isConcurrent", jdbcType = JdbcType.BIT),
            @Result(column = "execute_plan_data", property = "executePlanData", jdbcType = JdbcType.VARCHAR),
            @Result(column = "node_arr", property = "nodeArr", jdbcType = JdbcType.VARCHAR)
    })
    TaskInvokeVo modifyByTaskInvokeId(String taskInvokeId);

    @Update({
            "update tb_ssgp_task_invoke",
            "set status = #{status,jdbcType=BIT}",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    int changeInvokeStatus(TaskInvoke taskInvoke);

    //@UpdateProvider(type=TaskInvokeSqlProvider.class, method="deleteInvoke")
    @Update({
            "update tb_ssgp_task_invoke",
            "set is_used = #{isUsed,jdbcType=BIT}",
            "where task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR}"
    })
    int deleteInvoke(TaskInvoke invoke);

    @SelectProvider(type = TaskInvokeSqlProvider.class, method = "selectSelective")
    @Results({
            @Result(column = "task_invoke_id", property = "taskInvokeId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "class_name", property = "className", jdbcType = JdbcType.VARCHAR),
            @Result(column = "method_name", property = "methodName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "params", property = "params", jdbcType = JdbcType.VARCHAR),
            @Result(column = "invoke_name", property = "invokeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),
            @Result(column = "start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "is_used", property = "isUsed", jdbcType = JdbcType.TINYINT),
            @Result(column = "failed_strategy", property = "failedStrategy", jdbcType = JdbcType.VARCHAR),
            @Result(column = "updata_time", property = "updataTime", jdbcType = JdbcType.TINYINT),
            @Result(column = "is_concurrent", property = "isConcurrent", jdbcType = JdbcType.BIT),
            @Result(column = "execute_plan_data", property = "executePlanData", jdbcType = JdbcType.VARCHAR),
    })
    TaskInvoke selectSelective(TaskInvoke taskInvoke);

    @Update({
            "<script>",
            "UPDATE tb_ssgp_task_invoke",
            "SET status = #{status}",
            "WHERE task_invoke_id in ",
            "<foreach item='item' index='index' collection='params' open='(' separator=',' close=')'>",
            "#{item.taskInvokeId} ",
            "</foreach>",
            "</script>"
    })
    int batchUpdateStatus(@Param("params") List<UpdateInvokeStatusParams> params, @Param("status") int status);
}